Thread: [HW] Bit rotation

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    14

    Question [HW] Bit rotation

    Hi,

    Im trying to create a bit rotate function for a "hexadecimal calculator". I found a code in wikipedia that seems to work:

    Code:
    unsigned int x;  unsigned int y;  /* ... */  y = (x << shift) | (x >> (sizeof(x)*CHAR_BIT - shift));
    However, i want to rewrite it on my own but i want to know the formula behind it, like what is "CHAR_BIT" for? Is it the number of bits the answer will be shown?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    sizeof(x) tells you the number of bytes in x
    CHAR_BIT tells you the number of bits in a byte (it's usually 8)
    The whole thing tells you the number of bits in x
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    Thanks for the reply.

    ok i played with it a bit using hexadecimal 49 and 10. I got the same answer (49000) even though i just used:

    (x << shift) | (x >> (x - shift)).

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Except your formula is not correct.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    ok, i was able to get a solution for the bit wise rotation without using the sizeof() function for rotate right:

    Code:
    temp = n1 & 0x1;
    n1 >>= 1;
    n1 = n1 | (temp << 31);
    however, im still curious about sizeof(). i tried hexadecimal "num1 = a" shift by "num2 = 5" but used just the "sizeof(num1)*8 - num2" part of the forumla.. i got 1b. im using unsigned int and got "4" from sizeof(num1) regardless of the value in num1. i just want to know the logic behind it since it was not very well explained in the wiki page it came from.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Warzaw View Post
    im using unsigned int and got "4" from sizeof(num1) regardless of the value in num1.
    That's because sizeof doesn't care about the value. It only looks at the type and gives you the size in bytes for that type in your implementation. So for your system, an unsigned int uses 4 bytes thus the possible values are 0 to (2^32 -- 1) [= 4294967295]. But the C standard only says that the maximum value for an unsigned int must be at least (2^16 - 1) [= 65535] so there can be systems where an unsigned int is stored in only 2 bytes and systems where an unsigned int is stored in more than 4 bytes.

    Hence, you use sizeof if you want to write portable programs.

    See also sizeof - Wikipedia, the free encyclopedia

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    By portable do you mean 32-bit and 64-bit systems?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Salem explained what sizeof does in post 2; It is replaced with a constant at compile-time, whose value is the number of bytes that the thing specified would occupy.

    Congratulations, and int is 4 bytes on your system!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    ok, i understand the formula now. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit rotation...
    By Junior89 in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2007, 03:47 PM
  2. Rotation
    By peckitt99 in forum Windows Programming
    Replies: 4
    Last Post: 03-25-2007, 06:46 AM
  3. Rotation in 3D?
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 12-24-2006, 05:46 AM
  4. rotation in 3d
    By DavidP in forum Game Programming
    Replies: 3
    Last Post: 11-04-2003, 10:06 PM
  5. rotation
    By muttski in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-10-2002, 03:34 PM